/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package submarines.thread;

import submarines.Config;
import submarines.EaPlayer;
import submarines.Player;
import submarines.PlayerManager;
import submarines.notices.NotficationManager;

/**
 *
 * @author elad
 */
public class EaPlayerTurn {
    Player thisTurn;
    String thisThread;
    NotficationManager nManager;
    PlayerManager pManager;
    
    public EaPlayerTurn(PlayerManager p, NotficationManager n) {
        nManager = n;
        pManager = p;
    }
    
    public synchronized boolean playTurn(Player next) {
        if (pManager.numPlayersLeft() == 1)
            return false;
        thisThread = Thread.currentThread().getName();
        thisTurn = pManager.get(0);
        if ((thisThread.equals(thisTurn.getName())) && (pManager.hasGameBegun())) {
            if (thisTurn.getType().equals(Config.COMPUTER_PLAYER)) {
                EaPlayer p = (EaPlayer)thisTurn;
                p.playTurn(pManager, nManager);
                pManager.getNextPlayer();
                notifyAll();
            }
        } else {
            try {
                wait(2500);
            } catch (InterruptedException e) { }
        }
        return true;
    }
    
    public void setFirtPlayer(Player f) {
        thisTurn = f;
    }
}
